package com.fund.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fund.api.entity.TradeOrder;
import com.fund.common.enums.TradeType;
import com.fund.common.pojo.Result;
import com.fund.product.mapper.ShareHoldingMapper;
import com.fund.product.pojo.entity.ShareFlow;
import com.fund.product.pojo.entity.ShareHolding;
import com.fund.product.service.ShareFlowService;
import com.fund.product.service.ShareHoldingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ShareHoldingServiceImpl extends ServiceImpl<ShareHoldingMapper, ShareHolding> implements ShareHoldingService {


    @Autowired
    ShareFlowService shareFlowService;

    /**
     * 根据订单更新持有份额
     * @param tradeOrders
     * @return
     */
    @Override
    public Result<Boolean> updateShareHoldings(List<TradeOrder> tradeOrders) {
        //TODO 并非todo。这地方一个一个处理非常慢，有待优化，但项目期间内肯定是优化不了了，没得时间
        tradeOrders.forEach(tradeOrder -> {
            //查询基金、客户、银行卡都相等的份额数据，若是申购则增，若是赎回则减
            String purchaseSql = "share_total = share_total + " + tradeOrder.getShareQuantity();
            String redemptionSql = "share_total = share_total - " + tradeOrder.getShareQuantity();
            ShareHolding shareHolding = lambdaQuery()
                    .eq(ShareHolding::getFundId, tradeOrder.getFundId())
                    .eq(ShareHolding::getClientId, tradeOrder.getClientId())
                    .eq(ShareHolding::getCardId, tradeOrder.getCardId()).one();

            //顺便更新份额变化表
            ShareFlow shareFlow = new ShareFlow();

            //若存在则更新
            //注：因为在创建订单的时候已经处理过过度赎回的问题，所以此处不会出现过度
            if(shareHolding != null) {
                update().setSql(tradeOrder.getType() == TradeType.PURCHASE,purchaseSql)
                        .setSql(tradeOrder.getType() == TradeType.REDEMPTION,redemptionSql).update();
                shareFlow.setNewShare(shareHolding.getShareTotal() + tradeOrder.getShareQuantity());
            }else {//若不存在则新增，不存在的一定是申购
                ShareHolding newShareHolding = new ShareHolding();
                newShareHolding.setFundId(tradeOrder.getFundId());
                newShareHolding.setClientId(tradeOrder.getClientId());
                newShareHolding.setCardId(tradeOrder.getCardId());
                newShareHolding.setShareTotal(tradeOrder.getShareQuantity());
                save(newShareHolding);

                shareFlow.setNewShare(tradeOrder.getShareQuantity());
            }
            shareFlow.setFundId(tradeOrder.getFundId());
            shareFlow.setClientId(tradeOrder.getClientId());
            shareFlow.setCardId(tradeOrder.getCardId());
            shareFlow.setChangeTime(LocalDateTime.now());
            shareFlow.setChangeType(tradeOrder.getType());
            shareFlow.setChangeValue(tradeOrder.getShareQuantity());
            shareFlow.setTradeTime(tradeOrder.getCreateTime());
            shareFlow.setOperatorId(tradeOrder.getOperatorId());

            shareFlowService.save(shareFlow);
        });
        return Result.success(true);
    }
}
